package com.twu.biblioteca.menu;

import com.twu.biblioteca.exceptions.InvalidMenuSelectionException;
import com.twu.biblioteca.membership.MembershipFormatter;
import com.twu.biblioteca.model.BookFormatter;
import com.twu.biblioteca.model.BookList;
import com.twu.biblioteca.reservation.ReservationParser;
import com.twu.biblioteca.reservation.ReservationSystem;

import java.io.OutputStream;
import java.io.PrintStream;

public enum Menu {
    LIST_BOOKS {
        @Override
        public void execute() {
            BookList.getInstance().displayList(new BookFormatter(System.out));
        }
    }, RESERVE_BOOK {
        @Override
        public void execute() {
            ReservationSystem reservationSystem = new ReservationSystem();
            reservationSystem.reserve(BookList.getInstance(), new ReservationParser(System.in), System.out);
        }
    }, MEMBERSHIP_DETAILS {
        @Override
        public void execute() {
            MembershipFormatter membershipFormatter = new MembershipFormatter(System.out);
            membershipFormatter.checkMembership();
        }
    }, EXIT {
        @Override
        public void execute() {
            System.exit(0);
        }
    };

    public static void displayMenu(MenuFormatter menuFormatter){
        for (Menu item : Menu.values()){
            menuFormatter.addMenuItem(item.ordinal(), item.name());
        }
        menuFormatter.print();
    }

    public abstract void execute();

    public static void select(SelectionParser parser, PrintStream printStream){
        try {
            Menu menuItem = parser.parseInput();
            menuItem.execute();
        } catch (InvalidMenuSelectionException e) {
            printStream.println(e.getMessage());
        }
    }

}